import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.*;
import java.util.stream.Collectors;

import static java.lang.Integer.parseInt;

/**
 * @author CuiJian
 * Date    2021/10/17 13:40
 */
public class SteamDemo {
    public static void main(String[] args) {
        List<StudentBasicSupport> studentBasicSupports = Lists.newArrayList();
        StudentBasicSupport studentBasicSupport0 = new StudentBasicSupport("小白",23,"343");
        StudentBasicSupport studentBasicSupport1 = new StudentBasicSupport("小黑",90,"33");
        StudentBasicSupport studentBasicSupport2 = new StudentBasicSupport("小花",28,"125");
        StudentBasicSupport studentBasicSupport3 = new StudentBasicSupport("小绿",56,"409");
        StudentBasicSupport studentBasicSupport4 = new StudentBasicSupport("小蓝",56,"409");
        studentBasicSupports.add(studentBasicSupport1);
        studentBasicSupports.add(studentBasicSupport2);
        studentBasicSupports.add(studentBasicSupport3);
        studentBasicSupports.add(studentBasicSupport4);
        studentBasicSupports.add(studentBasicSupport0);
        System.out.println(studentBasicSupport0);

        /**按照某个条件将ListA中的某个属性添加到ListB中*/
        List<Integer> studentBasicSupports1 = studentBasicSupports.stream()
                .map(StudentBasicSupport :: getAge).collect(Collectors.toList());
        System.out.println(studentBasicSupports1);
        /**过滤filter*/
        List<StudentBasicSupport> studentBasicSupports2 = studentBasicSupports.stream()
                .filter(s -> s.getAge()>25).collect(Collectors.toList());
        studentBasicSupports2.forEach(s -> System.out.println(s.getName()));

        /**去重distinct（）*/
        List<Integer> studentBasicSupports3 = studentBasicSupports.stream().map(StudentBasicSupport::getAge)
                .distinct().collect(Collectors.toList());
        System.out.println(studentBasicSupports3);

        /**获取流的前n个元素limit*/
        List<StudentBasicSupport> studentBasicSupports4 = studentBasicSupports.stream()
                .filter(s -> s.getAge()>25).limit(2).collect(Collectors.toList());
        System.out.println(studentBasicSupports4);

        /**排序sort*/
            List<StudentBasicSupport> studentBasicSupports5 = studentBasicSupports.stream()
                    .sorted((s1,s2) -> s1.age-s2.age).collect(Collectors.toList());
        System.out.println(studentBasicSupports5);

        /**跳过元素skip*/
        List<StudentBasicSupport> studentBasicSupports6 = studentBasicSupports.stream()
                .skip(3).collect(Collectors.toList());
        System.out.println(studentBasicSupports6);

        /**映射流特殊操作*/
        /**ID转成integer后大于200的学生的年龄综合*/
        int ageSum = studentBasicSupports.stream().filter(s -> parseInt(s.getId())>200)
                .mapToInt(StudentBasicSupport::getAge).sum();
        System.out.println(ageSum);

        /**查找判断*/
        /**判断allMatch所有的年龄是否都大于50*/
        /**判断一部分anyMatch*/
        Boolean agea = studentBasicSupports.stream().allMatch(studentBasicSupport -> studentBasicSupport.getAge()>50);
        Boolean aaaw = studentBasicSupports.stream().anyMatch(studentBasicSupport -> studentBasicSupport.getName().length()>2);

        /**不存在满足noneMatch条件返回true*/
        Boolean ageaa = studentBasicSupports.stream().noneMatch(studentBasicSupport -> studentBasicSupport.getAge()>50);

        /**返回满足条件的第一个元素*/

        Optional<StudentBasicSupport> fr = studentBasicSupports.stream().filter(s -> s.age>50).findFirst();
        StudentBasicSupport studentBasicSupport = fr.get();
        System.out.println(studentBasicSupport);

        /**拼接joining*/
        String name = studentBasicSupports.stream().map(StudentBasicSupport::getName)
                .collect(Collectors.joining(","));
        System.out.println(name);

       /* Map<String, Integer> studentMap  = new HashMap();
        Integer aa = 34;

        List<StudentBasicSupport> studentBasicSupportList;
        studentBasicSupportList = studentBasicSupports
                .stream()
                .filter(!(StudentBasicSupport::getAge <= aa))
                .collect(Collectors.toList());*/

        String aaaaa =
                studentBasicSupports
                        .stream()
                        .map(StudentBasicSupport ::getName)
                        .collect(Collectors.joining("%%"));
        System.out.println("年龄综合" + aaaaa);

        List<String> stringList = new ArrayList<>();
        Map<String, String> stringMap = Maps.newHashMap();
    }
}
